package com.ohuang.test_compose

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.ohuang.test_compose.bean.ActivityAction
import com.ohuang.test_compose.composeView.PageSlice
import com.ohuang.test_compose.composeView.VisibilityLayout
import com.ohuang.test_compose.page.PagingListPage
import com.ohuang.test_compose.page.testList
import com.ohuang.test_compose.page.textList
import com.ohuang.test_compose.ui.theme.Custom_Theme
import com.ohuang.test_compose.ui.theme.Test_composeTheme
import java.lang.Thread.sleep

@ExperimentalPermissionsApi
@InternalComposeApi
@ExperimentalMaterialApi
@ExperimentalAnimationApi
@ExperimentalUnitApi
class MainActivity : ComponentActivity() {

    private val vm: MainViewModel by viewModels()

    @SuppressLint("UnrememberedMutableState", "RememberReturnType")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ContextUtil.attach(this)

        setContent {
            Test_composeTheme() {
                PageSlice(vm.pageSliceManager){
                    mainPage()
                }
            }
        }
    }


    @Composable
    fun mainPage() {
        Box(Modifier.fillMaxHeight()) {
            val select by vm.select.observeAsState(0)
            val page0LastLazyListState = rememberLazyListState()
            val page1LastLazyListState = rememberLazyListState()

            Scaffold(modifier = Modifier.fillMaxHeight(),
                topBar = {
                    TopBar(
                        title = when (select) {
                            0 -> "首页"
                            1 -> "首页2"
                            2 -> "首页3"
                            else -> "其他"
                        }
                    )
                },
                bottomBar = {
                    Navigation(
                        select = select,
                        modifier = Modifier.align(Alignment.BottomCenter),
                        callBack = {
                            vm.select.value = it
                        }, color = MaterialTheme.colors.secondary,
                        color2 = MaterialTheme.colors.primary
                    )
                },
                content = {
                    Box(
                        modifier = Modifier
                            .align(Alignment.TopCenter)
                            .padding(bottom = it.calculateBottomPadding())
                    ) {
                        VisibilityLayout (isVisibility = select==0){
                            textList(page1LastLazyListState, vm.actionList) { c ->
                                if (c is ActivityAction){
                                    InstanceActivity.content=c.content
                                    InstanceActivity.start(ContextUtil.getContext()!!)
                                }else {
                                    vm.pageSliceManager.start(content = c.content)
                                }
                            }
                        }
                        VisibilityLayout (isVisibility = select==1){   var list = vm.halou.observeAsState(vm.list)
                            testList(page0LastLazyListState, list.value) {
                                sleep(1000)
                                vm.addFirstItem("new item ${System.currentTimeMillis() / 1000 % 10000}")
                            }}
                        VisibilityLayout (isVisibility = select==2){
                            PagingListPage(viewModel = vm)
                        }
                        VisibilityLayout (isVisibility = select==3){
                            EmptyView("other")
                        }

                    }
                }
            )
        }
    }


    override fun onResume() {
        super.onResume()
        Log.d("TAG", "onResume: ")
    }


    override fun onBackPressed() {

        if (!vm.pageSliceManager.goBack()) {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ContextUtil.clear()
    }


}


@ExperimentalUnitApi
@Composable
fun TopBar(title: String) {
    Column(
        Modifier
            .fillMaxWidth()
            .height(40.dp)
            .background(color = MaterialTheme.colors.primary),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(
            text = title, color = Color.White, fontSize = TextUnit(20F, TextUnitType.Sp)
        )
    }

}


@Composable
fun EmptyView(s: String) {
    Column() {
        Text(text = s)
    }
}

@Composable
fun NavigationButton(
    title: String,
    drawableId: Int,
    callBack: () -> Unit,
    modifier: Modifier,
    color: Color = Color(0xFFF44336)
) {

    Column(
        modifier = modifier.clickable { callBack() },
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Image(
            bitmap = ImageBitmap.Companion.imageResource(id = drawableId),
            modifier = Modifier
                .width(40.dp)
                .height(40.dp)
                .padding(4.dp),
            contentDescription = title,
            alignment = Alignment.Center
        )
        Text(
            text = title,
            textAlign = TextAlign.Center,
            modifier = Modifier
                .padding(top = 3.dp, bottom = 3.dp)
                .height(20.dp),
            color = color
        )
    }
}

@Composable
fun Navigation(
    select: Int,
    color: Color = Color(0xFFE91E63),
    color2: Color = Color(0xFF2196F3),
    callBack: (Int) -> Unit = {},
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .height(70.dp)
    ) {
        NavigationButton(
            title = "首页",
            R.drawable.icon_hj,
            { callBack(0) },
            modifier = Modifier.weight(1f),
            if (select == 0) color2 else color
        )
        NavigationButton(
            title = "首页2",
            R.drawable.icon_hj,
            { callBack(1) },
            modifier = Modifier.weight(1f),
            if (select == 1) color2 else color
        )
        NavigationButton(
            title = "首页3",
            R.drawable.icon_hj,
            { callBack(2) },
            modifier = Modifier.weight(1f), if (select == 2) color2 else color
        )

    }
}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    Test_composeTheme {
        Navigation(1, Color.Black, Color.Red)
    }
}